package com.cicadamob.contact.api.group.asynctask;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Handler;

import com.cicadamob.contact.api.Utils.ContactConfig;
import com.cicadamob.contact.api.Utils.Logger.AppLogger;
import com.cicadamob.contact.api.Utils.NetWork.HttpRequestCallBack;
import com.cicadamob.contact.api.Utils.NetWork.HttpUtil;
import com.cicadamob.contact.api.Utils.NetWork.NetWorkUtil;
import com.cicadamob.contact.api.Utils.NetWork.HttpUtil.RequestMethod;
import com.cicadamob.contact.api.group.bean.ResultModel;
import com.cicadamob.contact.api.group.dao.Group_Db_Dao;
import com.cicadamob.contact.api.group.http.Http_Group_Util;
import com.cicadamob.contact.api.group.http.JsonParseUtil;
import com.cicadamob.contact.api.service.Contact;
import com.cicadamob.contact.api.service.ServerUrl;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;

public class DelMemberRunnable implements Runnable {
	private Handler mHandler;
	private int what;
	private Context mContext;
	private Contact mContact;
	private String groupID;
	private Group_Db_Dao dbHelper;

	public DelMemberRunnable(Handler mHandler, int what, Context mContext,
			String groupID, Contact mContact) {
		this.mHandler = mHandler;
		this.what = what;
		this.mContext = mContext;
		this.mContact = mContact;
		this.groupID = groupID;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		dbHelper = Group_Db_Dao.getInstance(mContext);
		ResultModel resultmodel = new ResultModel();
		if (!NetWorkUtil.isConnected(mContext)) {
			resultmodel.setRetcode(100);
			return;// 网络不异常
		}
		mHandler.post(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				List<Contact> delContacts = new ArrayList<Contact>();
				delContacts.add(mContact);
				

				String xmlData = Http_Group_Util.get_deleteGroupMemberXML(groupID,
						JsonParseUtil.contactsIdsTOjson(delContacts), "0");
				Map<String, String> requestMap = new HashMap<String, String>();
				requestMap.put(HttpUtil.XML, xmlData);
				 AppLogger.writeLogToFile(mContext, ContactConfig.DIRECTORY+"/httpGroupLog", "msg_group", "发送报文  url =" + ServerUrl.DEL_GROUP_MEMBER
				    		+"\r\n xml ="+xmlData, true);
				HttpUtil.HttpRequest(mContext, RequestMethod.POST,
						ServerUrl.DEL_GROUP_MEMBER, false, requestMap,
						new HttpRequestCallBack<String>() {

							@Override
							public void onSuccess(ResponseInfo<String> responseInfo) {
								// TODO Auto-generated method stub
								doResult(responseInfo.result);
								AppLogger.writeLogToFile(mContext, ContactConfig.DIRECTORY+"/httpGroupLog", "msg_group", "响应报文  url =" + ServerUrl.DEL_GROUP_MEMBER
							    		+"\r\n xml ="+responseInfo.result, true);	
							}

							@Override
							public void onFailure(HttpException error, String msg) {
								// TODO Auto-generated method stub
								ResultModel resultmodel =new ResultModel();
								resultmodel.setRetcode(-8);
								sendMessage(resultmodel);
							}

						});
			}
		});
		
		

	}

	private void sendMessage(ResultModel mResult) {
		mHandler.obtainMessage(what, mResult).sendToTarget();
	}

	private void doResult(String result) {
		ResultModel resultmodel = new ResultModel();

		resultmodel = Http_Group_Util.parseResult(result);
		if (resultmodel.getRetcode() == 0 || resultmodel.getRetcode() == 3) {
			dbHelper.delMemberByMid(mContact.getIdInGroup());
			resultmodel.setResultData(dbHelper.queryAllMemberByGroupId(groupID));
		}
		sendMessage(resultmodel);

//		// 发送广播通知 群组消息更新相关信息
//		if (resultmodel.getRetcode() == 0 || resultmodel.getRetcode() == 3) {
//			Group group = new Group();
//			group.setGroup_id(groupID);
//			List<Contact> memberList = new ArrayList<Contact>();
//			memberList.add(mContact);
//			PushMessageModel message = new PushMessageModel();
//			message.setGroupName(dbHelper.queryGroupNameAndImage(groupID)
//					.getGroup_name());
//			message.setGroupUserName(mContact.getDisplayName());
//			String tips = PushUtils.getNoticeTips(4, message);
//			PushUtils
//					.MsgSyncGroupOrMember(m, 3, group, memberList, tips, false);
//		}

	}

}
